package protocol.JT808_2011;

import java.util.HashMap;

public class Handle808_2011JT86 implements IHandle808_2011JT {
    @Override
    public String handle(JT808_2011Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle8600(cmd);
            case 0x01:
                return handle8601(cmd);
            case 0x02:
                return handle8602(cmd);
            case 0x03:
                return handle8603(cmd);
            case 0x04:
                return handle8604(cmd);
            case 0x05:
                return handle8605(cmd);
            case 0x06:
                return handle8606(cmd);
            case 0x07:
                return handle8607(cmd);
            default:
                return "808-2011:未匹配的命令字"+(cmd.readOrder());
        }
    }

    private String handle8607(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除线路");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("路线总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    路线ID:" + cmd.readWORD());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8606(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置线路");
        builder.append("\r\n");

        builder.append("线路ID:"+cmd.readDWORD());
        builder.append("\r\n");
        int value = cmd.readWORD();
        builder.append("线路属性:"+value);
        builder.append("\r\n");

        boolean isByTime = (value&0x01) == 1;
        builder.append("    根据时间(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    进线路报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进线路报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出线路报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出线路报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");

        if (isByTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        int num = cmd.readWORD();
        builder.append("路线拐点项数:"+num);
        builder.append("\r\n");
        for (int i = 0; i < num; i++) {
            builder.append("    拐点ID:"+cmd.readDWORD());
            builder.append("\r\n");
            builder.append("    路段ID:"+cmd.readDWORD());
            builder.append("\r\n");

            builder.append("    拐点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    拐点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    路段宽度:"+cmd.readInt());
            builder.append("\r\n");
            int value2 = cmd.readWORD();
            builder.append("    路段属性:"+value2);
            builder.append("\r\n");

            boolean isLimitTime = (value&0x01) == 1;
            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        根据时间(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>3&0x01));
            builder.append("\r\n");

            if (isLimitTime){
                builder.append("    路段行驶过长阈值:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    路段行驶不足阈值:"+cmd.readWORD());
                builder.append("\r\n");
            }

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值）:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    路段超速持续时间:"+cmd.readWORD());
                builder.append("\r\n");
            }
        }

        return builder.toString();
    }

    private String handle8605(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除多边形区域");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWORD());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8604(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置多边形区域");
        builder.append("\r\n");

        builder.append("    区域ID:"+cmd.readInt(4));
        builder.append("\r\n");
        int value = cmd.readWORD();
        builder.append("    属性:"+value);
        builder.append("\r\n");

        boolean isLimitTime = (value&0x01) == 1;

        boolean isLimitSpeed = (value>>1&0x01) == 1;
        builder.append("        是否启用起始时间和结束时间的判断规则(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("        限速(1有效):"+(value>>1&0x01));
        builder.append("\r\n");
        builder.append("        进区域报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("        进区域报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("        出区域报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("        出区域报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");
        builder.append("        0-北纬,1-南纬:"+(value>>6&0x01));
        builder.append("\r\n");
        builder.append("        0-东经,1-西经:"+(value>>7&0x01));
        builder.append("\r\n");

        if (isLimitTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        if (isLimitSpeed){
            builder.append("    最高速度（超速限值，千米每小时）:"+cmd.readWORD());
            builder.append("\r\n");

            builder.append("    超速持续时间（秒）:"+cmd.readInt());
            builder.append("\r\n");
        }

        int pointNum = cmd.readWORD();
        builder.append("顶点数量:"+pointNum);
        builder.append("\r\n");

        for (int i = 0; i < pointNum; i++) {
            builder.append("    顶点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    顶点经度:"+cmd.readInt(4));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle8603(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除矩形区域");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWORD());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8602(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置矩形区域");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"更新区域");
        list.put(1,"追加区域");
        list.put(2,"修改区域");
        builder.append("设置属性:"+list.getOrDefault(cmd.readInt(),"未定义"));
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:"+cmd.readInt(4));
            builder.append("\r\n");
            int value = cmd.readWORD();
            builder.append("    属性:"+value);
            builder.append("\r\n");

            boolean isLimitTime = (value&0x01) == 1;

            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        是否启用起始时间和结束时间的判断规则(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给驾驶员(1有效)"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给平台(1有效):"+(value>>3&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给驾驶员(1有效):"+(value>>4&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给平台:"+(value>>5&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>6&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>7&0x01));
            builder.append("\r\n");

            builder.append("    左上点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    左上点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    右下点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    右下点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            if (isLimitTime){
                builder.append("    起始时间:"+cmd.readBCD(6));
                builder.append("\r\n");
                builder.append("    结束时间:"+cmd.readBCD(6));
                builder.append("\r\n");
            }

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值，千米每小时）:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    超速持续时间（秒）:"+cmd.readInt());
                builder.append("\r\n");
            }
        }
        return builder.toString();
    }

    private String handle8601(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除圆形区域");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWORD());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8600(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置圆形区域");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"更新区域");
        list.put(1,"追加区域");
        list.put(2,"修改区域");
        builder.append("设置属性:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:"+cmd.readInt(4));
            builder.append("\r\n");
            int value = cmd.readWORD();
            builder.append("    属性:"+value);
            builder.append("\r\n");

            boolean isLimitTime = (value&0x01) == 1;

            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        是否启用起始时间和结束时间的判断规则(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给驾驶员(1有效)"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给平台(1有效):"+(value>>3&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给驾驶员(1有效):"+(value>>4&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给平台:"+(value>>5&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>6&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>7&0x01));
            builder.append("\r\n");

            builder.append("    中心点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    中心点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    半径:"+cmd.readDWORD());
            builder.append("\r\n");

            if (isLimitTime){
                builder.append("    起始时间:"+cmd.readBCD(6));
                builder.append("\r\n");
                builder.append("    结束时间:"+cmd.readBCD(6));
                builder.append("\r\n");
            }

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值）:"+cmd.readWORD());
                builder.append("\r\n");

                builder.append("    超速持续时间:"+cmd.readInt());
                builder.append("\r\n");
            }
        }

        return builder.toString();
    }

}
